package com.blog.vo;

import com.blog.entity.ArticleEntity;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.beans.BeanUtils;

import java.io.Serializable;

/**
 * @Author：CreateSequence
 * @Date：2020-08-15 20:26
 * @Description：文章详情VO
 */
@Data
@NoArgsConstructor
public class ArticleDetailVO implements Serializable {

    private Long articleId;

    private String articleTitle;

    private String articleContent;

    /**
     * 下一篇文章ID
     */
    private long nextArticleId;

    /**
     * 下一篇文章标题
     */
    private String nextArticleTitle;

    /**
     * 上一篇文章ID
     */
    private long prevArticleId;

    /**
     * 上一篇文章标题
     */
    private String prevArticleTitle;

    /**
     * 将当前文章和上一篇下一篇文章的标题和ID组合为VO
     * @param target 当前要展示的文章
     * @param next 上一篇文章
     * @param prev 下一篇文章
     */
    public ArticleDetailVO(ArticleEntity target,ArticleHelperVO next,ArticleHelperVO prev) {
        if (target == null) {
            throw new RuntimeException("要查询的文章不存在或被删除！");
        }
        BeanUtils.copyProperties(target,this);
        if (next == null) {
            this.nextArticleId = -1;
            this.nextArticleTitle = "没有下一篇文章了";
        }else {
            this.nextArticleId = next.getArticleId();
            this.nextArticleTitle = next.getArticleTitle();
        }

        if (prev == null) {
            this.prevArticleId = -1;
            this.prevArticleTitle = "没有上一篇文章了";
        }else {
            this.prevArticleId = prev.getArticleId();
            this.prevArticleTitle = prev.getArticleTitle();
        }

    }
}
